/**
请实现一个函数，把字符串 s 中的每个空格替换成"%20"。 

 

 示例 1： 

 输入：s = "We are happy."
输出："We%20are%20happy." 

 

 限制： 

 0 <= s 的长度 <= 10000 

 Related Topics 字符串 👍 450 👎 0

*/

package com.shuzijun.leetcode.editor.cn;
public class TiHuanKongGeLcof{
    public static void main(String[] args) {
         Solution solution = new TiHuanKongGeLcof().new Solution();
        solution.replaceSpace("we are happy.");
    }
    //leetcode submit region begin(Prohibit modification and deletion)
class Solution {
    public String replaceSpace(String s) {
        int n = s.length();

        int countSpace = 0;
        for (int i = 0; i < n; i++) {
            if (s.charAt(i) == ' '){
                countSpace++;
            }
        }

        int newLen = n + countSpace * 2;
        char[] newStr = new char[newLen];
        int j = newLen-1;
        for (int i = n-1; i >= 0; i--) {
            char c = s.charAt(i);
            if (c != ' '){
                newStr[j] = c;
                j--;
            }else {
                newStr[j] = '0';
                newStr[j-1] = '2';
                newStr[j-2] = '%';
                j -= 3;

            }
        }

        return new String(newStr);
    }
}
//leetcode submit region end(Prohibit modification and deletion)

}